package be.bejug.scrumadmin.components.panels;

import java.util.Set;
import java.util.TreeSet;

import org.springframework.beans.factory.annotation.Autowired;

import be.bejug.scrumadmin.domain.DomainObject;
import be.bejug.scrumadmin.domain.Sprint;
import be.bejug.scrumadmin.domain.Story;
import be.bejug.scrumadmin.domain.Task;
import be.bejug.scrumadmin.domain.User;
import be.bejug.scrumadmin.domain.enums.Priority;
import be.bejug.scrumadmin.services.BacklogService;
import be.bejug.scrumadmin.services.SprintService;
import be.bejug.scrumadmin.spring.Injector;

import com.vaadin.addon.treetable.TreeTable;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

/**
 * 
 * @author Glenn Dejaeger
 * 
 */
@SuppressWarnings("serial")
public class BacklogPanel extends AbstractTab implements ClickListener {

	private TreeTable backlogTable;

	@Autowired
	private transient BacklogService backLogService;

	@Autowired
	private transient SprintService sprintService;

	/**
	 * 
	 */
	public BacklogPanel() {
		Injector.inject(this);
		initComponents();
	}

	@Override
	protected String getCaptionKey() {
		return "panel.title.backlog";
	}

	protected void initComponents() {
		initBacklogTable();
	}

	/**
	 * 
	 */
	private void initBacklogTable() {
		backlogTable = new TreeTable();

		backlogTable.addContainerProperty("Name", String.class, null);
		backlogTable.addContainerProperty("Description", String.class, null);
		backlogTable.addContainerProperty("Priority", Priority.class, null);
		backlogTable.addContainerProperty("Estimation", Double.class, null);
		backlogTable.addContainerProperty("Created by", User.class, null);

		addComponent(backlogTable);

		backlogTable.setSelectable(true);
		backlogTable.setMultiSelect(true);
		backlogTable.setColumnReorderingAllowed(true);
		backlogTable.setSizeFull();
		setExpandRatio(backlogTable, 1.0f);
	}

	/**
	 * @return the backLogService
	 */
	public BacklogService getBackLogService() {
		return backLogService;
	}

	/**
	 * @param backLogService
	 *            the backLogService to set
	 */
	public void setBackLogService(final BacklogService backLogService) {
		this.backLogService = backLogService;
	}

	@Override
	protected void loadData() {
		Set<Story> backlog = new TreeSet<Story>(backLogService.getBacklog());
		for (Story story : backlog) {
			Object storyItemId = backlogTable.addItem(
					new Object[] { story.getName(), story.getDescription(),
							story.getPriority(), story.getEstimation(),
							story.getCreatedBy() }, story);
			for (Task task : story.getTasks()) {
				Object taskItemId = backlogTable.addItem(
						new Object[] { task.getName(), task.getDescription(),
								task.getPriority(), task.getEstimation(),
								task.getCreatedBy() }, task);
				backlogTable.setParent(taskItemId, storyItemId);
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void buttonClick(final ClickEvent event) {
		Set<DomainObject> values = ((Set<DomainObject>) backlogTable.getValue());
		Sprint s = sprintService.getCurrentSprint();
		for (DomainObject object : values) {
			if (object instanceof Story) {
				s.addStories(((Story) object));
			}
		}
	}
}
